package com.tzj.pic;


import com.tzj.tg.BasePicture;
import com.tzj.tg.Blockable;
import com.tzj.tg.Config;
import com.tzj.tg.Destroyable;
import com.tzj.tg.Dircetion;
import com.tzj.tg.Hitable;
import com.tzj.tg.Moveable;

import utils.CollsionUtils;

/**
 * @author honeyrr
 *	坦克
 */
public class Tank extends BasePicture implements Moveable, Blockable, Destroyable, Hitable{
	
	private int blood = 3;
	private Dircetion dircetion = Dircetion.UP;
	private int speed = 32;
	/**
	 * 构造函数
	 * @param x
	 * @param y
	 */
	public Tank(int x, int y) {
		super(x, y);
		int[] size = Config.getImgSize(Config.TANK3_UP);
		this.width = size[0];
		this.height = size[1];
	}

	/**
	 * 绘制
	 */
	@Override
	public void draw() {
		
		switch (this.dircetion) {
		case UP:
			Config.draw(Config.TANK3_UP, x, y);
			break;
			
		case DOWN:
			Config.draw(Config.TANK3_DOWN, x, y);
			break;
		
		case LEFT:
			Config.draw(Config.TANK3_LEFT, x, y);
			break;
			
		case RIGHT:
			Config.draw(Config.TANK3_RIGHT, x, y);
			break;
			
		default:
			break;
		}
	}

	public void move(Dircetion dircetion) {
		if (this.flag && this.dircetion == dircetion) {
			Config.play(Config.HIT);
			return;
		}
		if (this.dircetion != dircetion) {
			this.dircetion = dircetion;
			return;
		}
		switch (dircetion) {
		case UP:
			y -= speed;
			break;
			
		case DOWN:
			y += speed;
			break;
		
		case LEFT:
			x -= speed;
			break;
			
		case RIGHT:
			x += speed;
			break;
			
		default:
			break;
		}
		
		if(x <= 0) { x = 0; }
		if(y <= 0) { y = 0; }
		if(x >= Config.WIDTH - 64) { x = Config.WIDTH - 64; }
		if(y >= Config.HEIGHT - 64) { y = Config.HEIGHT - 64; }
	}
	
	/*
	 * 坦克发射子弹:400ms发射一次
	 */
	long lastTime;
	public Bullet shotBullet() {
		long currentTime = System.currentTimeMillis();
		if (currentTime - lastTime < 100) {
			return null;
		} else {
			lastTime = currentTime;
		}
		
		Bullet bullet = new Bullet(this);
		return bullet;
	}

	@Override
	protected int getOrder() {
		// TODO 自动生成的方法存根
		return 1;
	}
	
	// 是否撞墙铁水的标记，如果是就不移动
	private boolean flag = false;
	@Override
	public boolean moveHit(Blockable bp2) {
		if (this == bp2) {
			return false;
		}
		BasePicture bp = (BasePicture) bp2;
		int x1 = bp.x;
		int y1 = bp.y;
		int w1 = bp.width;
		int h1 = bp.height;
		
		int x2 = this.x;
		int y2 = this.y;
		int w2 = this.width;
		int h2 = this.height;
		
		switch (this.dircetion) {
		case UP:
			y2 -= speed;
			break;
			
		case DOWN:
			y2 += speed;
			break;
		
		case LEFT:
			x2 -= speed;
			break;
			
		case RIGHT:
			x2 += speed;
			break;
			
		default:
			break;
		}
		
		flag = CollsionUtils.isCollsionWithRect(x1, y1, w1, h1, x2, y2, w2, h2);
		return flag;
	}
	
	public Dircetion getDircetion() { return dircetion; }

	@Override
	public Blast showBlast() {
		this.blood--;
		Blast blast = new Blast(this, this.blood <= 0);
		return blast;
	}

	@Override
	public boolean isDestroyed() {
		return this.blood <= 0;
	}
	
	
}
